package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.FaceRecognitionReq;
import sz.szsmk.citizencard.model.network.bean.request.GetOrderOpenid;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.GetOrderOpenidRes;
import sz.szsmk.citizencard.mvp.contract.IAPPMainContact;
import sz.szsmk.citizencard.mvp.contract.IFaceAuthContract;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2019
 * author: zdh
 * created on: 2019/1/8 11:04
 * description:
 */
public class FaceAuthPresenter extends IBasePresenter
        implements IFaceAuthContract.IFaceAuthPresenter<IFaceAuthContract.IFaceAuthView> {

    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public FaceAuthPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void faceResult(String photo, String custName, String idCard) {
        FaceRecognitionReq faceRecognitionReq = new FaceRecognitionReq();
        faceRecognitionReq.setActual_name(custName);
        faceRecognitionReq.setId_no(Constant.USER_IDCARD);
        faceRecognitionReq.setPhoto(photo);
        String json = new Gson().toJson(faceRecognitionReq);
        L.e(Constant.TAG, "人脸识别入参报文：" + json);
        mNetworkDateSource.faceRecognition(faceRecognitionReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                ((IFaceAuthContract.IFaceAuthView) mView).faceResultSucc(response);
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
//                Toast.makeText(DaggerApplication.getIns(), response.getMessage(), Toast.LENGTH_SHORT).show();
                ((IFaceAuthContract.IFaceAuthView) mView).faceResultFail(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void getGetOrderId() {
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");

        GetOrderOpenid getOrderOpenid = new GetOrderOpenid();
        getOrderOpenid.setTag("HUIMIN");
        getOrderOpenid.setAccess_token(access_token);
        String json = new Gson().toJson(getOrderOpenid);
        L.e(Constant.TAG, "getOrderOpenid----->" + json);
        mNetworkDateSource.getOrderOpenid(getOrderOpenid, new SubscribeResult<GetOrderOpenidRes>() {
            @Override
            protected void onOk(GetOrderOpenidRes response) {
                String toJson = new Gson().toJson(response);
                Constant.EXTENT = response.getExtend();
                ACache.get(DaggerApplication.getIns()).put("mextend", response.getExtend());
                (  (IFaceAuthContract.IFaceAuthView)mView).getGetOrderIdSucc(response );
            }

            @Override
            protected void onNext(GetOrderOpenidRes response) {
                super.onNext(response);
                String toJson = new Gson().toJson(response);
                L.e(Constant.TAG, "-------------" + toJson);
            }

            @Override
            protected void onFailure(GetOrderOpenidRes response) {
                super.onFailure(response);
                String toJson = new Gson().toJson(response);
                if (response.getCode().equals("0000")) {

                    ACache.get(DaggerApplication.getIns()).remove("mextend");
                    ACache.get(DaggerApplication.getIns()).put("mextend", response.getExtend());
                }
                L.e(Constant.TAG, "-------------" + toJson);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }




}
